#include "sensor_proc.h"
#include <esp_log.h>
#include "system_init.h"
#include <freertos/FreeRTOS.h>

#define TAG "sensor"

SensorQMI8658 qmi;



void qmi8658a_init(void)
{
    if(!qmi.begin(esp_i2c_bus_handle_get(0), QMI8658_H_SLAVE_ADDRESS)) {
        ESP_LOGE(TAG, "Failed to initialize QMI8658A sensor");
        return;
    } else {
        ESP_LOGI(TAG, "QMI8658A sensor initialized successfully");
    }

    ESP_LOGI(TAG, "QMI Device ID: 0x[%02X]", qmi.getChipID());
    // accele self test
    if (qmi.selfTestAccel()) {
        ESP_LOGI(TAG, "Accelerometer self-test passed");
    } else {
        ESP_LOGE(TAG, "Accelerometer self-test failed");
    }
    // gyro self test
    if (qmi.selfTestGyro()) {
        ESP_LOGI(TAG, "Gyroscope self-test passed");
    } else {
        ESP_LOGE(TAG, "Gyroscope self-test failed");
    }

    qmi.configAccelerometer(
        /*
         * ACC_RANGE_2G
         * ACC_RANGE_4G
         * ACC_RANGE_8G
         * ACC_RANGE_16G
         * */
        SensorQMI8658::ACC_RANGE_4G,
        /*
         * ACC_ODR_1000H
         * ACC_ODR_500Hz
         * ACC_ODR_250Hz
         * ACC_ODR_125Hz
         * ACC_ODR_62_5Hz
         * ACC_ODR_31_25Hz
         * ACC_ODR_LOWPOWER_128Hz
         * ACC_ODR_LOWPOWER_21Hz
         * ACC_ODR_LOWPOWER_11Hz
         * ACC_ODR_LOWPOWER_3H
        * */
        SensorQMI8658::ACC_ODR_1000Hz,
        /*
        *  LPF_MODE_0     //2.66% of ODR
        *  LPF_MODE_1     //3.63% of ODR
        *  LPF_MODE_2     //5.39% of ODR
        *  LPF_MODE_3     //13.37% of ODR
        *  LPF_OFF        // OFF Low-Pass Fitter
        * */
        SensorQMI8658::LPF_MODE_0);




    qmi.configGyroscope(
        /*
        * GYR_RANGE_16DPS
        * GYR_RANGE_32DPS
        * GYR_RANGE_64DPS
        * GYR_RANGE_128DPS
        * GYR_RANGE_256DPS
        * GYR_RANGE_512DPS
        * GYR_RANGE_1024DPS
        * */
        SensorQMI8658::GYR_RANGE_64DPS,
        /*
         * GYR_ODR_7174_4Hz
         * GYR_ODR_3587_2Hz
         * GYR_ODR_1793_6Hz
         * GYR_ODR_896_8Hz
         * GYR_ODR_448_4Hz
         * GYR_ODR_224_2Hz
         * GYR_ODR_112_1Hz
         * GYR_ODR_56_05Hz
         * GYR_ODR_28_025H
         * */
        SensorQMI8658::GYR_ODR_896_8Hz,
        /*
        *  LPF_MODE_0     //2.66% of ODR
        *  LPF_MODE_1     //3.63% of ODR
        *  LPF_MODE_2     //5.39% of ODR
        *  LPF_MODE_3     //13.37% of ODR
        *  LPF_OFF        // OFF Low-Pass Fitter
        * */
        SensorQMI8658::LPF_MODE_3);
    /*
    * If both the accelerometer and gyroscope sensors are turned on at the same time,
    * the output frequency will be based on the gyroscope output frequency.
    * The example configuration is 896.8HZ output frequency,
    * so the acceleration output frequency is also limited to 896.8HZ
    * */
    qmi.enableGyroscope();
    qmi.enableAccelerometer();
    // qmi.dumpCtrlRegister();
    IMUdata acc;
    IMUdata gyr;
    // delay 100ms
    vTaskDelay(pdMS_TO_TICKS(100));
    if (qmi.getDataReady()) {
        if (qmi.getAccelerometer(acc.x, acc.y, acc.z)) {
            ESP_LOGI(TAG, "ACCEL.x: %f, ACCEL.y: %f, ACCEL.z: %f", acc.x, acc.y, acc.z);
        }

        if (qmi.getGyroscope(gyr.x, gyr.y, gyr.z)) {
            ESP_LOGI(TAG, "GYRO.x: %f, GYRO.y: %f, GYRO.z: %f", gyr.x, gyr.y, gyr.z);
        }
    }
}